package edu.zqu.instructionalMatters.project.controller;

import edu.zqu.common.result.ZQUResult;
import edu.zqu.common.status.HttpStatus;
import edu.zqu.instructionalMatters.project.VO.CurriculumResultVO;
import edu.zqu.instructionalMatters.project.entity.CurriculumResult;
import edu.zqu.instructionalMatters.project.mapper.CurriculumResultMapper;
import edu.zqu.instructionalMatters.project.util.CurriculumResultUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.math.BigInteger;
import java.util.ArrayList;
import java.util.List;

/**
 * @Author 何文东
 * @Date 2020-07-03
 * @Description: 课表执行结果 - 前端控制器
 * @Version 1.0
 */
@RestController
@RequestMapping("/api")
@Api(tags = "课表执行结果")
public class CurriculumResultController {
    @Autowired
    private CurriculumResultMapper curriculumResultMapper;
    @Autowired
    private CurriculumResultUtil curriculumResultUtil;

    @ApiOperation(value = "获取课表执行结果列表")
    @RequestMapping(value = "/curriculumResults", method = RequestMethod.GET)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "collegeId", value = "学校id", paramType = "query", required = true),
            @ApiImplicitParam(name = "schoolId", value = "校区id", paramType = "query", required = false),
            @ApiImplicitParam(name = "curriculumId", value = "课表id", paramType = "query", required = false),
            @ApiImplicitParam(name = "statusId", value = "结果字典id", paramType = "query", required = false)
    })
    public ZQUResult getList(BigInteger collegeId,
                             BigInteger schoolId,
                             BigInteger curriculumId,
                             Integer statusId
    ) {
        CurriculumResult curriculumResult1 = new CurriculumResult()
                .setCollegeId(collegeId).setSchoolId(schoolId).setCurriculumId(curriculumId).setStatusId(statusId);     //合成 CurriculumResult 对象
        List<CurriculumResult> list1 = curriculumResultMapper.findAll(curriculumResult1);                               //获取List<CurriculumResult>
        List<CurriculumResultVO> list = new ArrayList<>();                                                              //初始化List<CurriculumResultVO>
        for (CurriculumResult curriculumResult : list1) {                                                                //循环遍历
            CurriculumResultVO curriculumResultVO = curriculumResultUtil.curriculumResultUtil(curriculumResult);        //调用工具类完成赋值
            list.add(curriculumResultVO);                                                                               //添加到list
        }
        return ZQUResult.success(list);                                                                                 //返回list
    }

    @ApiOperation(value = "获取课表执行结果信息", response = CurriculumResultVO.class)
    @GetMapping(value = "/curriculumResult/{id}")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "id", paramType = "path", required = true)
    })
    public ZQUResult getById(@PathVariable BigInteger id) {
        CurriculumResult curriculumResult = curriculumResultMapper.selectById(id);                                      //课程变动申请对象
        CurriculumResultVO curriculumResultVO = curriculumResultUtil.curriculumResultUtil(curriculumResult);            //调用工具类赋值
        return new ZQUResult(HttpStatus.SUCCESS, "请求成功", curriculumResultVO);                                  //数据返回
    }

    @ApiOperation(value = "添加")
    @RequestMapping(value = "/curriculumResult", method = RequestMethod.POST)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "collegeId", value = "学校id", paramType = "query", required = true),
            @ApiImplicitParam(name = "schoolId", value = "校区id", paramType = "query", required = true),
            @ApiImplicitParam(name = "curriculumId", value = "课表id", paramType = "query", required = true),
            @ApiImplicitParam(name = "statusId", value = "结果字典id", paramType = "query", required = true),
            @ApiImplicitParam(name = "calendarId", value = "校历id", paramType = "query", required = true),
            @ApiImplicitParam(name = "timeTableId", value = "作息id", paramType = "query", required = true),
            @ApiImplicitParam(name = "instructionClassId", value = "授课班级id", paramType = "query", required = true),
            @ApiImplicitParam(name = "courseId", value = "课程id", paramType = "query", required = true),
            @ApiImplicitParam(name = "fieldId", value = "场地id", paramType = "query", required = true),
            @ApiImplicitParam(name = "teacherId", value = "教师id", paramType = "query", required = true),
            @ApiImplicitParam(name = "byWho", value = "用户id", paramType = "query", required = true)
    })
    public ZQUResult add(BigInteger collegeId,
                         BigInteger schoolId,
                         BigInteger curriculumId,
                         Integer statusId,
                         BigInteger calendarId,
                         BigInteger timeTableId,
                         BigInteger instructionClassId,
                         BigInteger courseId,
                         BigInteger fieldId,
                         BigInteger teacherId,
                         BigInteger byWho) {
        CurriculumResult curriculumResult = new CurriculumResult();               //初始化
        curriculumResult.setCollegeId(collegeId);                                 //设置学校id
        curriculumResult.setSchoolId(schoolId);                                   //设置校区id
        curriculumResult.setCurriculumId(curriculumId);                           //设置课表id
        curriculumResult.setStatusId(statusId);                                   //设置课表执行结果字典id
        curriculumResult.setCalendarId(calendarId);                               //设置校历id
        curriculumResult.setTimeTableId(timeTableId);                             //设置作息表id
        curriculumResult.setInstructionClassId(instructionClassId);               //设置授课班级id
        curriculumResult.setCourseId(courseId);                                   //设置课程id
        curriculumResult.setFieldId(fieldId);                                     //设置授课场地id
        curriculumResult.setTeacherId(teacherId);                                 //设置教师id
        curriculumResult.setByWho(byWho);                                         //设置操作者
        curriculumResultMapper.insert(curriculumResult);                          //执行插入
        return ZQUResult.success();                                               //操作成功返回
    }

    @ApiOperation(value = "更新")
    @RequestMapping(value = "/curriculumResult/{id}", method = RequestMethod.PUT)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "collegeId", value = "学校id", paramType = "query", required = true),
            @ApiImplicitParam(name = "id", value = "id", paramType = "path", required = true),
            @ApiImplicitParam(name = "schoolId", value = "校区id", paramType = "query", required = true),
            @ApiImplicitParam(name = "curriculumId", value = "课表id", paramType = "query", required = true),
            @ApiImplicitParam(name = "statusId", value = "结果字典id", paramType = "query", required = true),
            @ApiImplicitParam(name = "calendarId", value = "校历id", paramType = "query", required = true),
            @ApiImplicitParam(name = "timeTableId", value = "作息id", paramType = "query", required = true),
            @ApiImplicitParam(name = "instructionClassId", value = "授课班级id", paramType = "query", required = true),
            @ApiImplicitParam(name = "courseId", value = "课程id", paramType = "query", required = true),
            @ApiImplicitParam(name = "fieldId", value = "场地id", paramType = "query", required = true),
            @ApiImplicitParam(name = "teacherId", value = "教师id", paramType = "query", required = true),
            @ApiImplicitParam(name = "byWho", value = "用户id", paramType = "query", required = true)
    })
    public ZQUResult update(BigInteger collegeId,
                            @PathVariable BigInteger id,
                            BigInteger schoolId,
                            BigInteger curriculumId,
                            Integer statusId,
                            BigInteger calendarId,
                            BigInteger timeTableId,
                            BigInteger instructionClassId,
                            BigInteger courseId,
                            BigInteger fieldId,
                            BigInteger teacherId,
                            BigInteger byWho) {
        CurriculumResult curriculumResult = curriculumResultMapper.selectById(id);//先查出旧记录
        curriculumResult.setByWho(byWho);                                         //设置操作者
        curriculumResultMapper.updateById(curriculumResult);                      //更新操作者
        curriculumResultMapper.deleteById(id);                                    //逻辑删除
        curriculumResult = new CurriculumResult();                                //初始化
        curriculumResult.setCollegeId(collegeId);                                 //设置学校id
        curriculumResult.setSchoolId(schoolId);                                   //设置校区id
        curriculumResult.setCurriculumId(curriculumId);                           //设置课表id
        curriculumResult.setStatusId(statusId);                                   //设置课表执行结果字典id
        curriculumResult.setCalendarId(calendarId);                               //设置校历id
        curriculumResult.setTimeTableId(timeTableId);                             //设置作息表id
        curriculumResult.setInstructionClassId(instructionClassId);               //设置授课班级id
        curriculumResult.setCourseId(courseId);                                   //设置课程id
        curriculumResult.setFieldId(fieldId);                                     //设置授课场地id
        curriculumResult.setTeacherId(teacherId);                                 //设置教师id
        curriculumResult.setByWho(byWho);                                         //设置操作者
        curriculumResult.setLastVersion(id);                                      //设置上一版本
        curriculumResultMapper.insert(curriculumResult);                          //执行插入
        return ZQUResult.success();                                               //操作成功返回
    }

    @ApiOperation(value = "删除")
    @RequestMapping(value = "/curriculumResult/{id}", method = RequestMethod.DELETE)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "id", paramType = "path", required = true),
            @ApiImplicitParam(name = "byWho", value = "用户id", paramType = "query", required = true)
    })
    public ZQUResult delete(@PathVariable BigInteger id,
                            BigInteger byWho) {
        CurriculumResult curriculumResult = curriculumResultMapper.selectById(id);//先查出旧记录
        curriculumResult.setByWho(byWho);                                         //设置操作者
        curriculumResultMapper.updateById(curriculumResult);                      //更新操作者
        curriculumResultMapper.deleteById(id);                                    //逻辑删除
        return ZQUResult.success();                                               //操作成功返回
    }
}

